﻿using Abp.Application.Services;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.UI;
using AutoMapper;
using CheryABP.Authorization;
using CheryABP.Person.Dto;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheryABP.Person
{
    [ApiExplorerSettings(IgnoreApi = true)]
    public class PersonService : ApplicationService
    {
        private readonly IPersonRepository _personRepository;
        private readonly IRepository<People> _ppRepository;
        private readonly IRepository<Department> _depRepository;
        /// <summary>
        /// 构造函数自动注入
        /// </summary>
        /// <param name="personRepository"></param>
        public PersonService(IPersonRepository personRepository, IRepository<People> ppRepository,IRepository<Department> depRepository)
        {
            _personRepository = personRepository;
            _ppRepository = ppRepository;
            _depRepository = depRepository;
        }
        /// <summary>
        /// 查询多个实体集合
        /// </summary>
        /// <returns></returns>
        //[AbpAuthorize(PermissionNames.Pages_Users)]
        public GetMultiPeopleOutput GetMultitData()
        {
            var persons = _ppRepository.GetAll();
            var dd = _depRepository.GetAll();
            //用AutoMapper自动将List<Peopson>转换成List<PeopsonDto>
            return new GetMultiPeopleOutput
            {
                 People= Mapper.Map<List<PersonDto>>(persons),
                 dep = Mapper.Map<List<DepartmentDto>>(dd)
            };
        }
        /// <summary>
        /// 获取全部人员信息
        /// </summary>
        /// <returns></returns>
        public List<PersonDto> GetAllPerson()
        {
            var persons = _ppRepository.GetAll();
            //用AutoMapper自动将List<Peopson>转换成List<PeopsonDto>
            return persons.MapTo<List<PersonDto>>();
        }

        /// <summary>
        /// 获取全部人员信息,自定义返回格式，页数
        /// </summary>
        /// <returns></returns>
        public GetAllPeopleOutput GetAllPersonOut()
        {
            var persons = _ppRepository.GetAll().ToList();
            return new GetAllPeopleOutput { PageCount =50,People= persons };
        }

        /// <summary>
        /// 根据id查询人员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        public PersonDto GetPersonByID(int id)
        {
            //var persons = _personRepository.GetPersonByID(id);
            //return persons;
            var pp =_ppRepository.Get(id);
            return pp.MapTo<PersonDto>();
        }
        /// <summary>
        /// 传实体插入人员数据
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public string InsertPeople(People pp)
        {
            int aa = _ppRepository.InsertAndGetId(pp);
            if (aa > 0)
            {
                return "数据写入成功";
            }
            throw new UserFriendlyException("失败！");
        }
        /// <summary>
        /// 前端传入json
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public string InsertPeopleString(string json)
        {
            var actionModel = JsonConvert.DeserializeObject<People>(json);
            int aa = _ppRepository.InsertAndGetId(actionModel);
            if (aa > 0)
            {
                return "数据写入成功";
            }
            throw new UserFriendlyException("失败！");
        }
        /// <summary>
        /// 写入部门数据
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
      
        public string InsertDepartment(Department pp)
        {
            int aa = _depRepository.InsertAndGetId(pp);
            if (aa > 0)
            {
                return "部门写入成功";
            }
            throw new UserFriendlyException("失败！");
        }
        /// <summary>
        /// 更新人员信息
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public string UpdatePerson(People pp)
        {
            var aa = _ppRepository.Update(pp);
            if (aa!=null)
            {
                return "更新成功";
            }
            throw new UserFriendlyException("失败！");
        }
        /// <summary>
        /// 更新人员信息by   id
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public string UpdatePersonString(int id,string name)
        {
            var aa = _ppRepository.Update(id,i=>i.Name=name);
            if (aa != null)
            {
                return "更新成功";
            }
            throw new UserFriendlyException("失败！");
        }

        /// <summary>
        /// 更新人员信息返回最新信息
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public PersonDto UpdatePersonBack(int id, string name)
        {
            var aa = _ppRepository.Update(id, i => i.Name = name);
            return aa.MapTo<PersonDto>();
        }
        /// <summary>
        /// 默认实现UnitOfWork，更新数据，即使不调用 update方法
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        ///[UnitOfWork(IsDisabled = true)]禁用工作单元
        public void UpdateUnitOfWork(int id,string name)
        {
            var person = _ppRepository.Get(id);
            person.Name = name;
        }
        /// <summary>
        /// 删除人员by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string DeletePerson(int id)
        {
            try
            {
                _ppRepository.Delete(id);
                return "删除成功了";
            }
            catch (Exception ex)
            {
                throw new UserFriendlyException(ex.Message);
            }
        }
        /// <summary>
        /// ado 展示人员关联部门
        /// </summary>
        /// <returns></returns>
        public DataTable GetPersonDep()
        {
           return  _personRepository.GetPersonDep();
        }

       
        
    }

}
